package com.dxq.test.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ObjectUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.dxq.test.App;
import com.dxq.test.common.AppManager;
import com.dxq.test.R;
import com.dxq.test.adapter.VaccineAdapter;
import com.dxq.test.db.Group;
import com.dxq.test.db.Label;
import com.dxq.test.db.Pasture;
import com.dxq.test.db.TablesDao;
import com.dxq.test.db.Vaccine;
import com.j256.ormlite.dao.ForeignCollection;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: zsh
 * Date: 2023/11/17
 * Describe: 防治列表页面
 */
public class VaccineActivity extends AppCompatActivity implements View.OnClickListener {

    public TablesDao tablesDao;

    private ImageView ivBack, ivMore;
    private TextView tvTitle, tvSet;
    private RecyclerView recyclerView;

    private VaccineAdapter mAdapter;
    private List<Vaccine> recordList = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //添加Activity到堆栈
        AppManager.getAppManager().addActivity(this);
        setContentView(R.layout.activity_vaccine);
        tablesDao = new TablesDao(this);
        initView();
        initAdapter();
    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在appManager结束当前activity
        AppManager.getAppManager().finishActivity(this);
    }

    private void initView() {
        ivBack = findViewById(R.id.ivBack);
        ivMore = findViewById(R.id.ivMore);
        tvSet = findViewById(R.id.tvSet);
        tvTitle = findViewById(R.id.tvTitle);
        tvTitle.setText("防治列表");
        tvSet.setText("添加");
        tvSet.setVisibility(View.VISIBLE);

        recyclerView = this.findViewById(R.id.recyclerView);

        ivBack.setOnClickListener(this);
        tvSet.setOnClickListener(this);
    }

    private void initAdapter() {
        recyclerView.setLayoutManager(new GridLayoutManager(this, 1));
//        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));//添加分割线
        mAdapter = new VaccineAdapter(R.layout.item_vaccine, recordList);//
        recyclerView.setAdapter(mAdapter);
        mAdapter.setEmptyView(R.layout.layout_empty);

        //item点击事件
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter,
                                    @NonNull @NotNull View view, int position) {
                //ToastUtils.showShort("item事件");
                Intent intent = new Intent(VaccineActivity.this, VaccineAddActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("vaccine", recordList.get(position));
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

        //item子view点击事件
        mAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull @NotNull BaseQuickAdapter adapter,
                                         @NonNull @NotNull View view, int position) {
                //ToastUtils.showShort("child添加事件");

            }
        });
    }

    private void getData() {
        recordList.clear();
        Pasture pasture = tablesDao.queryPastureById(App.select_pasture_id);
        if (null != pasture) {
            ForeignCollection<Vaccine> vaccines = pasture.getVaccines();
            if (null != vaccines && vaccines.size() > 0) {
                for (Vaccine vaccine : vaccines) {
                    //查询防治个体
                    List<Label> labels = tablesDao.queryLabelForVaccine(vaccine);
                    if (!ObjectUtils.isEmpty(labels)) {
                        vaccine.setLabel(labels.get(0));
                        recordList.add(vaccine);
                    }
                    //查询防治分群
                    List<Group> groups = tablesDao.queryGroupForVaccine(vaccine);
                    if (!ObjectUtils.isEmpty(groups)) {
                        vaccine.setGroup(groups.get(0));
                        recordList.add(vaccine);
                    }
                }
            }
        }
        //Collections.sort(recordList, Comparat);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.ivBack:
                VaccineActivity.this.finish();
                break;

            case R.id.tvSet:
                Intent intent = new Intent(VaccineActivity.this, VaccineAddActivity.class);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void onBackPressed() {
        this.finish();
    }
}
